package com.tyqx.hashchainbackend.service.impl;
import com.tyqx.hashchainbackend.service.SysUserService;
import com.tyqx.hashchainbean.module.SysUser;
import com.tyqx.hashchainbean.vo.SysUpdaUserPwdVO;
import com.tyqx.hashchainbean.vo.SysUserModifyVO;
import com.tyqx.hashchainbean.vo.SysUserSaveVO;
import com.tyqx.hashchaindao.mapper.SysUserMapper;
import hashchainutil.common.BackendPage;
import hashchainutil.common.Constants;
import hashchainutil.common.DigestUtil;
import hashchainutil.common.EmptyUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    public SysUser getById(Long userId) throws Exception {
        return sysUserMapper.getById(userId);
    }

    @Override
    public SysUser getByUserCode(String userCode) throws Exception {
        return sysUserMapper.getSysUserByUserCode(userCode);
    }

    public List<SysUser> getListByMap(Map<String, Object> param) throws Exception {
        return sysUserMapper.getListByMap(param);
    }

    public Integer getCountByMap(Map<String, Object> param) throws Exception {
        return sysUserMapper.getCountByMap(param);
    }

    public Integer save(SysUserSaveVO sysUserSaveVO) throws Exception {
        if(EmptyUtils.isEmpty(sysUserSaveVO.getUserActivate())){
            sysUserSaveVO.setUserActivate(0);
        }
       sysUserSaveVO.setUserCreationDate(new Date());
       sysUserSaveVO.setUserPassword(DigestUtil.hmacSign(sysUserSaveVO.getUserPassword()));
        return sysUserMapper.save(sysUserSaveVO);
    }

    public Integer modify(SysUserModifyVO sysUserModifyVO) throws Exception {
//        sysUser.setUserModifyDate(new Date());
//        String password = DigestUtil.hmacSign(sysUser.getUserPassword());
//        sysUser.setUserPassword(password);
        return sysUserMapper.modify(sysUserModifyVO);
    }

    public Integer removeById(Long userId) throws Exception {
        return sysUserMapper.removeById(userId);
    }

    public BackendPage<List<SysUser>> queryPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = sysUserMapper.getCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        param.put("beginPos", (pageNo - 1) * pageSize);
        param.put("pageSize", pageSize);
        List<SysUser> sysUserList = sysUserMapper.getSysUserListByMap(param);
        BackendPage page = new BackendPage(total, sysUserList);
        return page;
    }




    @Override
    public Integer updateActivate(Integer userActivate, Long userId) {

        return sysUserMapper.updateActivate(userActivate,userId);
    }
    @Override
    public Integer getUserModify(SysUserModifyVO modifyVO)throws Exception {
        Subject currentUser = SecurityUtils.getSubject();
        SysUser currUser=(SysUser)currentUser.getSession().getAttribute("sysUser");

        SysUser user = new SysUser();
        user.setUserId(modifyVO.getUserId());
        user.setUserActivate(modifyVO.getUserActivate());
        user.setUserPhone(modifyVO.getUserPhone());
        user.setUserName(modifyVO.getUserName());
        user.setUserEmail(modifyVO.getUserEmail());
        user.setUserCode(modifyVO.getUserCode());
        user.setUserSex(modifyVO.getUserSex());
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        user.setUserModifyDate(dateString);
        user.setUserModifyBy(currUser.getUserId());
        return sysUserMapper.getUserModify(user);
    }

    @Override
    public Integer updateUserPwdById(SysUpdaUserPwdVO sysUpdaUserPwdVO) {
        String password = DigestUtil.hmacSign(sysUpdaUserPwdVO.getUserPassword());
        sysUpdaUserPwdVO.setUserPassword(password);
        return sysUserMapper.updateUserPwdById(sysUpdaUserPwdVO);
    }

    @Override
    public Integer delActivate(Integer userAcivate, Long userId) {
        return sysUserMapper.delActivate(userAcivate,userId);
    }


}
